from concurrent.futures import ThreadPoolExecutor
import time
import threading

def task(name):
    # 获取线程名称（threading.name）、线程的id（threading.ident）
    tname = threading.current_thread().name
    print(f" {tname} running Task {name} ")
    time.sleep(1)
    return f"Task {name} is done"

# 创建一个线程池，指定最大线程是5个
# ThreadPoolExecutor中的with会自动关闭线程池，并且会等待所有任务完成再退出上下文
with ThreadPoolExecutor(max_workers=5) as executor:
    # 提交任务
    futures = [executor.submit(task, i) for i in range(10)]
    for future in futures:
        # 获取任务的结果
        result = future.result()
        print(result)

print("所有任务已经完成")